//
//  SpeedDialAccess.m
//  LabanBrowser
//
//  Created by -Xh- on 3/14/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "SpeedDialAccess.h"

@implementation SpeedDialAccess
-(NSMutableArray *)getRecordsFromSelectQuery:(NSString *)_query{
    NSMutableArray* speedDialArray=[[NSMutableArray alloc]init];
    [database open];
    FMResultSet* queryResult=[database executeQuery:_query];
    while ([queryResult next]) {
        SpeedDialModel* currentSpeedDial=[[SpeedDialModel alloc]init];
        currentSpeedDial.title=[queryResult stringForColumn:@"Title"];
        currentSpeedDial.url=[queryResult stringForColumn:@"Url"];
        currentSpeedDial.favIcon=[queryResult dataForColumn:@"FavIcon"];
        currentSpeedDial.dateAdded=[queryResult dateForColumn:@"DateAdded"];
        currentSpeedDial.dateLastModified=[queryResult dateForColumn:@"DateLastModified"];
        [speedDialArray addObject:currentSpeedDial];
    }
    [database close];
    return speedDialArray;
}

-(NSMutableArray *)getAllRecords{
    NSString* queryString=[NSString stringWithFormat:@"SELECT * FROM SPEEDDIAL ORDER BY SPEEDDIAL.DateAdded ASC"];
    return [self getRecordsFromSelectQuery:queryString];
}

-(BOOL)insert:(BaseModel *)_object{
    BOOL success;
    SpeedDialModel* insertingSpeedDial=(SpeedDialModel*)_object;
    NSString* modifiedTitle=[insertingSpeedDial.title stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

    SpeedDialModel* recordOnDB=(SpeedDialModel*)[self findByURL:insertingSpeedDial.url];
    [database open];
    if(recordOnDB!=nil){
        NSString* updateQuery=[NSString stringWithFormat:@"UPDATE SPEEDDIAL SET DateLastModified='%@' AND Title = '%@' WHERE Url='%@'",insertingSpeedDial.dateLastModified,insertingSpeedDial.title, recordOnDB.url];
        success = [database executeUpdate:updateQuery];
        
    }else{
        NSString* insertQuery=[NSString stringWithFormat:@"INSERT INTO SPEEDDIAL (Title,DateAdded,DateLastModified,Url,FavIcon) VALUES ('%@','%@','%@','%@','%@')",modifiedTitle,insertingSpeedDial.dateAdded,insertingSpeedDial.dateLastModified,insertingSpeedDial.url,insertingSpeedDial.favIcon];
        success = [database executeUpdate:insertQuery];

    }
         
    
    [database close];
    return success;
}

-(SpeedDialModel *)findByURL:(NSString *)_url{
    //the result to return
    SpeedDialModel* currentSpeedDial=[[SpeedDialModel alloc]init];
    [database open];
    NSString* queryString=[NSString stringWithFormat:@"SELECT * FROM SPEEDDIAL WHERE Url='%@'",_url];
    FMResultSet* queryResult=[database executeQuery:queryString];
    if([queryResult next]){

        currentSpeedDial.title=[queryResult stringForColumn:@"Title"];
        currentSpeedDial.url=[queryResult stringForColumn:@"Url"];
        currentSpeedDial.favIcon=[queryResult dataForColumn:@"FavIcon"];
        currentSpeedDial.dateAdded=[queryResult dateForColumn:@"DateAdded"];
        currentSpeedDial.dateLastModified=[queryResult dateForColumn:@"DateLastModified"];

    }else{
        currentSpeedDial=nil;
    }
    [database close];
    return currentSpeedDial;
    
}


-(BOOL)updateObjectAtURL:(NSString *)_url withNewObject:(BaseModel *)_newObject{
    SpeedDialModel* newSpeedDial=(SpeedDialModel*)_newObject;
    [database open];
    NSString* updateQuery=[NSString stringWithFormat:@"UPDATE SPEEDDIAL SET Title='%@', DateLastModified='%@' WHERE Url='%@'",newSpeedDial.title,[NSDate date],_url];
    BOOL success = [database executeUpdate:updateQuery];
    [database close];
    return success;
}

-(BOOL)deleteObjectAtID:(NSString *)_id{
    BOOL success;
    [database open];
    NSString* deleteQuery=[NSString stringWithFormat:@"DELETE FROM SPEEDDIAL WHERE SpeedDialID='%@'",_id];
    success = [database executeUpdate:deleteQuery];
    [database close];
    return success;
}

-(BOOL)deleteObjectAtURL:(NSString *)_url{
    BOOL success;
    [database open];
    NSString* deleteQuery=[NSString stringWithFormat:@"DELETE FROM SPEEDDIAL WHERE Url='%@'",_url];
    success = [database executeUpdate:deleteQuery];
    [database close];
    return success;
}
@end
